﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using System.IO;

namespace Auktionshuset
{
    public partial class CreateAuctionWindow
    {
        private readonly Connection _conn;
        private byte[] _imageBytes;

        public CreateAuctionWindow(Connection conn)
        {
            _conn = conn;
    
            InitializeComponent();
        }

        private void CreateAuctionButton_Click(object sender, RoutedEventArgs e)
        {
            Auction auction = new Auction
            {
                Title = TitleTextBox.Text,
                Description = DescriptionTextBox.Text,

                ImageBytes = _imageBytes,

                NextBid = decimal.Parse(StartingPriceTextBox.Text),

                Starts = DateTimeDatePicker.DisplayDate + TimeSpanTimePicker.StartTime
            };

            _conn.PostAuction(auction);

            Close();
        }

        private void SelectImageButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog
            {
                Filter = ("Billedfiler (*.bmp, *.jpg, *.png, *.gif)|*.bmp;*.jpg;*.png;*.gif")
            };
            fileDialog.ShowDialog();
            string filePath = fileDialog.FileName;

            if (filePath != null)
            {
                _imageBytes = File.ReadAllBytes(filePath);

                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.UriSource = new Uri(filePath, UriKind.Absolute);
                src.CacheOption = BitmapCacheOption.OnLoad;
                src.EndInit();

                ProductPicture.Source = src;
            }
        }
    }
}
